package week3;
// 使用队列演示售票处模拟排队。

import java.util.*;

public class TicketCounter {
    final static int PROCESS = 120;
    final static int MAX_CASHIERS = 10;
    final static int NUM_CUSTOMERS = 100;

    public static void main(String[] args) {
        Customer customer;
        Queue<Customer> customerQueue = new LinkedList<Customer>();
        int[] cashierTime = new int[MAX_CASHIERS];
        int totalTime, averageTime, departs, start;

        // 模拟不同数量的收银员。
        for (int cashiers = 0; cashiers < MAX_CASHIERS; cashiers++)
        {
            // 初始化每个收银员的时间为0。
            for (int count = 0; count < cashiers; count++)
            {
                cashierTime[count] = 0;
            }

            // 加载客户队列
            for (int count = 1; count <= NUM_CUSTOMERS; count++)
            {
                customerQueue.add(new Customer(count * 15));
            }
            totalTime = 0;

            // 处理队列中的所有客户
            while (!(customerQueue.isEmpty()))
            {
                for (int count = 0; count <= cashiers; count++)
                {
                    if (!(customerQueue.isEmpty()))
                    {
                        customer = customerQueue.remove();
                        if (customer.getArrivalTime() > cashierTime[count])
                        {
                            start = customer.getArrivalTime();
                        }
                        else
                        {
                            start = cashierTime[count];
                        }
                        departs = start + PROCESS;
                        customer.setDepartureTime(departs);
                        cashierTime[count] = departs;
                        totalTime += customer.totalTime();
                    }
                }
            }

            // 输出模拟结果
            averageTime = totalTime / NUM_CUSTOMERS;
            System.out.println("Number of cashiers: " + (cashiers + 1));
            System.out.println("Average time: " + averageTime + "\n");
        }
    }
}
